package com.sbck.cs3.fragment;

import android.os.Bundle;

import android.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.sbck.cs3.MyClass.HeartData;
import com.sbck.cs3.MyGlobal;
import com.sbck.cs3.MyClass.MyMessage;
import com.sbck.cs3.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class DeviceRealtimeInfomationFragment extends Fragment {

    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device_realtime_infomation,container,false);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //EventBus
        if(!EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().register(this);
        }

        this.view = view;

        MyGlobal.socketUtil.GetHeart();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if(EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().unregister(this);
        }
    }

    private void ShowToast(String msg)
    {
        if(MyGlobal.IsPopToast.get())
            Toast.makeText(getActivity(),msg , Toast.LENGTH_SHORT).show();
    }

    //事件总线
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MyMessage myMessage) {
        switch (myMessage.messageCategory) {
            case SocketReceive:
                break;
            case SocketSend:
            case SocketError:
                return;
        }

        switch (myMessage.messageType) {
            case Heart:
                ListView lv_list = (ListView) view.findViewById(R.id.lv_list);

                lv_list.setAdapter(new MyAdapter());
                break;
        }

    }
    private class MyAdapter extends BaseAdapter
    {
        int count;
        public  MyAdapter()
        {
            count = MyGlobal.heartDataArrayList.size();
        }
        @Override
        public int getCount() {

            return  count;

        }

        @Override
        public Object getItem(int i) {
            return MyGlobal.heartDataArrayList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            View v1;
            if(view == null)
            {
                v1 = View.inflate(getActivity(),R.layout.item_realtime_info,null);
            }
            else
            {
                v1 = view;
            }
            TextView tvKey = (TextView) v1.findViewById(R.id.heart_key);
            TextView tvValue = (TextView) v1.findViewById(R.id.heart_value);

            HeartData hd =  MyGlobal.heartDataArrayList.get(i);
            tvKey.setText(hd.Key);
            tvValue.setText(hd.Value);
            return v1;
        }
    }
}
